﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using INSTA.Library;

namespace INSTA.BankSystem.DAL
{
    public class MappedProperty
    {
        private string  _propertyName;
        private string  _propertyFieldName;
        private bool    _isPrimaryKey;

        


        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="property">The property.</param>
        public MappedProperty(DataMapper mapper, PropertyInfo property )
        {
            // On affecte l'objet contenant la propriété mappée
            // à notre propriété "MappedObject".
            _mappedObject = mapper;

            // Et l'on initialise la valeur des autres propriétés.
            initProperties(property);
        }

        /// <summary>
        /// Initializes the properties.
        /// </summary>
        /// <param name="property">The property to work on.</param>
        private void initProperties(PropertyInfo property)
        {
            // On renseigne le nom de la propriété ..
            _propertyName = property.Name;


            // On récupère une référence typée vers l'attribut
            // de type "FieldAttribute" décorant notre propriété.
            FieldAttribute attribute = property.GetUniqueAttribute<FieldAttribute>();

            // On renseigne le nom du champ de la BDD auquel correspond
            // la propriété.
            _propertyFieldName = attribute.FieldName;

            // On renseigne lq valeur indiquant si la propriété en cours 
            // de traitement représente la clé primaire de la table mappée.
            _isPrimaryKey = attribute.IsPrimarykey;

        }

        /// <summary>
        /// The _mapped object
        /// </summary>
        private DataMapper _mappedObject;

        /// <summary>
        /// The _mapped object
        /// </summary>
        public DataMapper MappedObject
        {
            get             { return _mappedObject;     }
            private set     { _mappedObject = value;    }
        }

        /// <summary>
        /// Gets or sets the name of the property.
        /// </summary>
        public string PropertyName
        {
            get         { return _propertyName;     }
            private set { _propertyName = value;    }
        }

        /// <summary>
        /// Gets or sets the property value by dynamically calling
        /// the getters & setters of the property via reflection.
        /// </summary>
        public object PropertyValue
        {
            get { return MappedObject.GetPropertyValue(PropertyName);   }
            set { MappedObject.SetPropertyValue(PropertyName, value);   }
        }

        /// <summary>
        /// Gets or sets the name of the property field.
        /// </summary>
        public string PropertyFieldName
        {
            get             { return _propertyFieldName;    }
            protected set   { _propertyFieldName = value;   }
        }

        public bool IsPrimaryKey
        {
            get             { return _isPrimaryKey;         }
            protected set   { _isPrimaryKey = value; }
        }
    }
}
